"""
分页组件
"""


class Pagination(object):
    def __init__(self, current_page, all_count, base_url, query_params, per_page=20, pager_page_count=11):
        """
        分页初始化
        :param current_page: 当前页码
        :param all_count: 数据库中总条数
        :param base_url: 基础URL，生成页码的前缀
        :param query_params: QueryDict对象，内部含有所有当前url的原对象
        :param per_page: 单页显示数据条数
        :param pager_page_count: 页面上最多显示的页码数量
        """
        # 最多可以有多少分页
        pager_count, b = divmod(all_count, per_page)
        if b != 0:
            pager_count += 1
        # 设置最多的分页数
        self.pager_count = pager_count

        # 设置当前传入页数
        # 如果当前传入页数合法
        if current_page and current_page.isdigit():
            self.current_page = int(current_page)
            # 传入当前页为负数，设为1
            if self.current_page <= 0:
                self.current_page = 1
            # 比分页数还多，设置为最多的分页数
            elif self.current_page > pager_count:
                self.current_page = pager_count
        # 传入页数不合法设置为1
        else:
            self.current_page = 1
        # 数据库中数据总条数
        self.all_count = all_count
        # 基本url 用来拼接地址
        self.base_url = base_url
        # 原有的get请求的参数
        self.query_params = query_params
        # 每页显示条数
        self.per_page = per_page
        # 页面显示页码数量，其他如果还存在使用...代替
        self.pager_page_count = pager_page_count
        # 一半页面，用来判断左右是否有...
        half_pager_page_count = int(pager_page_count / 2)
        self.half_pager_page_count = half_pager_page_count

    @property
    def start(self):
        """
        获取起始索引
        :return:
        """
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        """
        获取结束索引
        :return:
        """
        return self.current_page * self.per_page

    def page_data(self):
        """
        生成HTML页码所需的数据
        :return: page_list 页码列表，包括上一页、显示页面、下一页的数据
            每一项数据格式为{"key": 索引值, "title": 标题, "url": url, "active": 是否是当前页}
        """
        # 如果数据 总页码pager_count < 页面显示页码pager_page_count
        if self.pager_count < self.pager_page_count:
            pager_start = 1
            pager_end = self.pager_count
        else:
            # 数据页码已经超过11
            # 判断： 如果当前页 <= 5(half_pager_page_count, 一半页码数)
            if self.current_page <= self.half_pager_page_count:
                pager_start = 1
                pager_end = self.pager_page_count
            else:
                # 如果： 当前页+5 > 总页码
                if (self.current_page + self.half_pager_page_count) > self.pager_count:
                    pager_end = self.pager_count
                    pager_start = self.pager_count - self.pager_page_count + 1
                else:
                    pager_start = self.current_page - self.half_pager_page_count
                    pager_end = self.current_page + self.half_pager_page_count

        page_list = []
        # 首页与上一页按钮设置
        if self.current_page <= 1:
            first_page = {"key": "first", "title": "首页", "url": "#", "active": False}
            prev_page = {"key": "prev", "title": "上一页", "url": "#", "active": False}
        else:
            self.query_params['page'] = 1
            first_page = {"key": "first", "title": "首页", "url": f"{self.base_url}?{self.query_params.urlencode()}",
                          "active": False}
            self.query_params['page'] = self.current_page - 1
            prev_page = {"key": "prev", "title": "上一页", "url": f"{self.base_url}?{self.query_params.urlencode()}",
                         "active": False}
        page_list.extend([first_page, prev_page])

        # 中间页设置
        for i in range(pager_start, pager_end + 1):
            self.query_params['page'] = i
            tpl = {"key": i, "title": i, "url": f"{self.base_url}?{self.query_params.urlencode()}", "active": False}
            # 如果是当前点击页，则设置active标志
            if self.current_page == i:
                tpl["active"] = True
            page_list.append(tpl)

        # 尾页与下一页按钮设置
        if self.current_page >= self.pager_count:
            next_page = {"key": "next", "title": "下一页", "url": "#", "active": False}
            tail_page = {"key": "last", "title": "尾页", "url": "#", "active": False}
        else:
            self.query_params['page'] = self.current_page + 1
            url = f"{self.base_url}?{self.query_params.urlencode()}"
            next_page = {"key": "next", "title": "下一页", "url": url, "active": False}
            self.query_params['page'] = self.pager_count
            url = f"{self.base_url}?{self.query_params.urlencode()}"
            tail_page = {"key": "last", "title": "尾页", "url": url, "active": False}
        page_list.extend([next_page, tail_page])
        return page_list
